package com.example.smartgreenhouse.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartgreenhouse.entity.FanOperationLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FanOperationLogMapper extends BaseMapper<FanOperationLog> {

    /*
     * 根据风扇ID查询操作记录（倒序排列）
     */
    @Select("SELECT * FROM fan_operation_log WHERE fan_id = #{fanId} ORDER BY operator_time DESC")
    List<FanOperationLog> selectByFanId(Long fanId);

    /*
     * 根据温室ID和操作类型查询最新一条记录
     */
    @Select("SELECT * FROM fan_operation_log " +
            "WHERE greenhouse_id = #{greenhouseId} AND operation_type = #{type} " +
            "ORDER BY operator_time DESC LIMIT 1")
    FanOperationLog selectLatestByGreenhouseAndType(
            @Param("greenhouseId") Long greenhouseId,
            @Param("type") String type
    );
}